home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / emula / arosdv19.lha / AROS / exec / availmem.c < prev    next >
C/C++ Source or Header  |  1996-10-24  |  4KB  |  142 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: availmem.c,v 1.6 1996/10/24 15:50:45 aros Exp $
  4.     $Log: availmem.c,v $
  5.     Revision 1.6  1996/10/24 15:50:45  aros
  6.     Use the official AROS macros over the __AROS versions.
  7.  
  8.     Revision 1.5  1996/09/13 17:51:22  digulla
  9.     Use IPTR
  10.  
  11.     Revision 1.4  1996/08/13 13:55:58  digulla
  12.     Replaced AROS_LA by AROS_LHA
  13.     Replaced some AROS_LH*I by AROS_LH*
  14.     Sorted and added includes
  15.  
  16.     Revision 1.3  1996/08/01 17:41:05  digulla
  17.     Added standard header for all files
  18.  
  19.     Desc:
  20.     Lang:
  21. */
  22. #include <exec/alerts.h>
  23. #include <exec/execbase.h>
  24. #include <aros/libcall.h>
  25.  
  26. /*****************************************************************************
  27.  
  28.     NAME */
  29.     #include <exec/memory.h>
  30.     #include <clib/exec_protos.h>
  31.  
  32.     AROS_LH1(ULONG, AvailMem,
  33.  
  34. /*  SYNOPSIS */
  35.     AROS_LHA(ULONG, attributes, D1),
  36.  
  37. /*  LOCATION */
  38.     struct ExecBase *, SysBase, 36, Exec)
  39.  
  40. /*  FUNCTION
  41.     Return either the total available memory or the largest available
  42.     chunk of a given type of memory.
  43.  
  44.     INPUTS
  45.     attributes - The same attributes you would give to AllocMem().
  46.  
  47.     RESULT
  48.     Either the total of the available memory or the largest chunk if
  49.     MEMF_LARGEST ist set in the attributes.
  50.  
  51.     NOTES
  52.     Due to the nature of multitasking the returned value may already
  53.     be obsolete if this function returns.
  54.  
  55.     EXAMPLE
  56.     Print the total available memory.
  57.  
  58.     printf("Free memory: %lu bytes\n",AvailMem(0));
  59.  
  60.     Print the size of the largest chunk of chip memory.
  61.  
  62.     printf("Largest chipmem chunk: %lu bytes\n",
  63.            AvailMem(MEMF_CHIP|MEMF_LARGEST));
  64.  
  65.     BUGS
  66.  
  67.     SEE ALSO
  68.  
  69.     INTERNALS
  70.  
  71.     HISTORY
  72.     15-10-95    created by m. fleischer
  73.  
  74. ******************************************************************************/
  75. {
  76.     AROS_LIBFUNC_INIT
  77.  
  78.     ULONG ret=0;
  79.     struct MemHeader *mh;
  80.  
  81.     /* Nobody else should access the memory lists now. */
  82.     Forbid();
  83.  
  84.     /* Get pointer to first memory header... */
  85.     mh=(struct MemHeader *)SysBase->MemList.lh_Head;
  86.     /* And follow the list. */
  87.     while(mh->mh_Node.ln_Succ!=NULL)
  88.     {
  89.         /*
  90.         The current memheader is OK if there's no bit in the
  91.         'attributes' that isn't set in the 'mh->mh_Attributes'.
  92.         MEMF_CLEAR, MEMF_REVERSE, MEMF_NO_EXPUNGE, MEMF_TOTAL and
  93.         MEMF_LARGEST are treated as if they were always set in
  94.         the memheader.
  95.         */
  96.         if(!(attributes&~(MEMF_CLEAR|MEMF_REVERSE|MEMF_NO_EXPUNGE
  97.             |MEMF_TOTAL|MEMF_LARGEST|mh->mh_Attributes)))
  98.         {
  99.         /* Find largest chunk? */
  100.         if(attributes&MEMF_LARGEST)
  101.         {
  102.             /*
  103.             Yes. Follow the list of MemChunks and set 'ret' to
  104.             each value that is bigger than all previous ones.
  105.             */
  106.             struct MemChunk *mc=mh->mh_First;
  107.             while(mc!=NULL)
  108.             {
  109. #if !defined(NO_CONSISTENCY_CHECKS)
  110.             /*
  111.                 Do some constistency checks:
  112.                 1. All MemChunks must be aligned to
  113.                    sizeof(struct MemChunk).
  114.                 2. The end (+1) of the current MemChunk
  115.                    must be lower than the start of the next one.
  116.             */
  117.             if(  ((IPTR)mc|mc->mc_Bytes)&(sizeof(struct MemChunk)-1)
  118.                ||(  (UBYTE *)mc+mc->mc_Bytes>=(UBYTE *)mc->mc_Next
  119.                   &&mc->mc_Next!=NULL))
  120.                 Alert(AT_DeadEnd|AN_MemoryInsane);
  121. #endif
  122.             if(mc->mc_Bytes>ret)
  123.                 ret=mc->mc_Bytes;
  124.             mc=mc->mc_Next;
  125.             }
  126.         }
  127.         else if(attributes&MEMF_TOTAL)
  128.             /* Determine total size. */
  129.             ret+=(STRPTR)mh->mh_Upper-(STRPTR)mh->mh_Lower;
  130.         else
  131.             /* Sum up free memory. */
  132.             ret+=mh->mh_Free;
  133.         }
  134.         mh=(struct MemHeader *)mh->mh_Node.ln_Succ;
  135.     }
  136.     /* All done. Permit dispatches and return. */
  137.     Permit();
  138.     return ret;
  139.     AROS_LIBFUNC_EXIT
  140. } /* AvailMem */
  141.  
  142.